package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.domain.exam.StudentGrade;
import edu.sdjzu.exam.mapper.exam.StudentGradesMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student-grades")
@RequiredArgsConstructor
@SaCheckLogin
@SaCheckRole("student")
public class StudentGradeController {

    private final StudentGradesMapper studentGradesMapper;

    /**
     * 学生查看自己的成绩记录
     */
    @GetMapping("/my")
    public CommonResponse<List<StudentGrade>> getMyGrades() {
        try {
            Long studentId = StpUtil.getLoginIdAsLong();
            // 这里需要添加一个新的mapper方法来查询学生的所有成绩
            List<StudentGrade> grades = studentGradesMapper.selectGradesByStudentId(studentId);
            return CommonResponse.success(grades);
        } catch (Exception e) {
            return CommonResponse.error("获取成绩失败：" + e.getMessage());
        }
    }

    /**
     * 学生查看特定考试的成绩详情
     */
    @GetMapping("/exam/{examId}")
    public CommonResponse<StudentGrade> getGradeByExam(@PathVariable Long examId) {
        try {
            Long studentId = StpUtil.getLoginIdAsLong();
            StudentGrade grade = studentGradesMapper.selectGradeByExamAndStudent(examId, studentId);
            if (grade == null) {
                return CommonResponse.error("未找到该考试的成绩记录");
            }
            return CommonResponse.success(grade);
        } catch (Exception e) {
            return CommonResponse.error("获取成绩失败：" + e.getMessage());
        }
    }
}